package impl

import (
	"context"
	compb "gitee.com/micro-services/protocol/common"
	pb "gitee.com/micro-services/protocol/tag/server"
	"micro.tag.server/do"
	"micro.tag.server/models"
	"strings"
	"time"
)

func CreateRoomTag(ctx context.Context, r *pb.CreateRoomTagReq) (*pb.CreateRoomTagRsp, error) {
	tag := &models.VsRSpecialty{
		RoomId:        r.RoomID,
		SpecialtyName: strings.Join(r.Tag, ","),
		CreatedTime:   time.Now().Unix(),
		UpdatedTime:   time.Now().Unix(),
	}

	err := do.CreateRoomTag(tag)
	if err != nil {
		return nil, err
	}

	return &pb.CreateRoomTagRsp{
		RetCode: 0,
		Msg:     Success,
	}, nil
}

func DeleteRoomTag(ctx context.Context, r *pb.DeleteRoomTagReq) (*pb.DeleteRoomTagRsp, error) {
	err := do.DeleteRoomTag(r.RoomID, r.TagID)
	if err != nil {
		return nil, err
	}

	return &pb.DeleteRoomTagRsp{
		RetCode: 0,
		Msg:     Success,
	}, nil
}

func RoomTags(ctx context.Context, r *pb.RoomTagsReq) ([]*compb.RoomTagInfo, error) {
	tags, err := do.RoomTags(r.RoomID)
	if err != nil {
		return nil, err
	}

	infos := make([]*compb.RoomTagInfo, 0)
	for _, v := range tags {
		infos = append(infos, &compb.RoomTagInfo{
			TagID:   v.Id,
			RoomID:  v.RoomId,
			TagName: strings.Split(v.SpecialtyName, ","),
		})
	}
	return infos, nil
}
